package com.cyt.web.controller.wx;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysUserVo;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.question.service.IQuestionUserService;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 学校通用用户模块-基础用户控制层
 *
 * @author han jun
 * @date 2019-8-29 13:41:23
 */
@RestController
public class WxEducationUserController extends BaseController {

    @Resource
    private IQuestionUserService questionUserService;
    @Resource
    private WxEducationUserService wxEducationUserService;

    /**
     * 统一跳转
     *
     * @return Model
     * @author han jun
     * @date 2019-8-29 13:41:23
     */
    @RequestMapping("/wx/wxeducationuser/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId) {
        ModelAndView view = new ModelAndView("/wx/wxeducationuser/"+pageName);
        view.addObject("applyId", applyId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param wxEducationUserVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-29 13:41:23
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/page", method = RequestMethod.POST)
    public ReturnResult page(HttpServletRequest request, WxEducationUserVo wxEducationUserVo, int page, int limit) {
        // 根据当前用户查询
        SysUser sysUser = acceptCurrentUser(request);
        wxEducationUserVo.setCreateUser(sysUser.getId());
        return wxEducationUserService.page(wxEducationUserVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-29 13:41:23
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id) {
        ModelAndView view = new ModelAndView("/wx/wxeducationuser/wxEducationUserEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxEducationUserService.selectByPrimaryKey(id));
        }
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wxEducationUserVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-29 13:41:23
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxEducationUserVo wxEducationUserVo) {
        return wxEducationUserService.addOrUpdate(wxEducationUserVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param wxEducationUserVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-29 13:41:23
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxEducationUserVo wxEducationUserVo) {
        return wxEducationUserService.remove(wxEducationUserVo, acceptCurrentUser(request));
    }

    /**
     * 更新状态
     * @param request
     * @param id
     * @param status
     * @author congcong tao
     * @date 2019/9/6 15:35
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "更新状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/updateStatus", method = RequestMethod.POST)
    public ReturnResult updateStatus(HttpServletRequest request, String id, String status) {
        SysUser sysUser = acceptCurrentUser(request);
        WxEducationUser wxEducationUser = wxEducationUserService.selectByPrimaryKey(id);
        wxEducationUser.setStatus(status);
        wxEducationUser.setUpdateTime(DateUtil.date());
        wxEducationUser.setUpdateUser(sysUser.getId());
        wxEducationUserService.updateByPrimaryKey(wxEducationUser);
        return new ReturnResult();
    }

    /**
     * 解绑用户
     * @param request
     * @param id 用户主键
     * @author congcong tao
     * @date 2019/9/6 15:35
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "更新状态", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/unbinding", method = RequestMethod.GET)
    public ReturnResult unbinding(HttpServletRequest request, String id) {
        SysUser sysUser = acceptCurrentUser(request);
        WxEducationUser wxEducationUser = wxEducationUserService.selectByPrimaryKey(id);
        wxEducationUser.setMemberId(null);
        wxEducationUser.setUpdateTime(DateUtil.date());
        wxEducationUser.setUpdateUser(sysUser.getId());
        wxEducationUserService.updateByPrimaryKey(wxEducationUser);
        // 解绑会将关于该用户的问卷全部变为未提交
        // 相当于发送命令过去，无关于成功与否
        questionUserService.updateStatusByUnbindUser(id);
        return new ReturnResult();
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param wxEducationUserVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-29 13:41:23
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WxEducationUserVo wxEducationUserVo) {
        return wxEducationUserService.delete(wxEducationUserVo, acceptCurrentUser(request));
    }

    /**
     * 重置密码
     * @param request
     * @param wxEducationUserVo
     * @author congcong tao
     * @date 2019/9/6 9:00
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "重置密码", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/resetPassword", method = RequestMethod.GET)
    public ReturnResult resetPassword(HttpServletRequest request, WxEducationUserVo wxEducationUserVo) {
        return wxEducationUserService.resetPassword(wxEducationUserVo, acceptCurrentUser(request));
    }

    /**
     * 校验工号唯一性，多账号下可以允许userNumber同名
     * @param id
     * @param userNumber
     * @author congcong tao
     * @date 2019/9/6 9:11
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "校验工号唯一性", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/checkUserNumberUnique", method = RequestMethod.POST)
    public ReturnResult checkUserNumberUnique(HttpServletRequest request, String id, String userNumber) {
        // 多账号下可以允许userNumber同名
        SysUser sysUser = acceptCurrentUser(request);
        String createUser = sysUser.getId();
        boolean flag = wxEducationUserService.checkUserNumberUnique(id, userNumber, createUser);
        if (flag) {
            return new ReturnResult();
        } else {
            return new ReturnResult(ErrorCode.CODE_1, "不存在");
        }
    }

    /**
     * 导入学生教师
     * @author congcong tao
     * @date 2019/9/6 8:44
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "导入学生教师", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/importData", method = RequestMethod.POST)
    public ReturnResult importData(HttpServletRequest request, MultipartFile file, String catalog, String update, String applyId) {
        return wxEducationUserService.importData(request, file, catalog, update, applyId, acceptCurrentUser(request));
    }

    /**
     *获取所有的教师角色
     *
     * @param keyWord 搜索关键字
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 17:18
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "获取所有教师角色", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/getAllTeacherList", method = RequestMethod.POST)
    public ReturnResult getAllTeacherList(String keyWord,String applyId) {
        WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
        wxEducationUserVo.setUserType("2");
        wxEducationUserVo.setName(keyWord);
        wxEducationUserVo.setApplyId(applyId);
        ReturnResult returnResult = wxEducationUserService.selector(wxEducationUserVo);
        return returnResult;
    }




    /**
     *获取所有的教师角色
     *
     * @param ids 搜索关键字
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 17:18
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "获取所有教师角色", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/getAllUserNumber", method = RequestMethod.POST)
    public ReturnResult getAllUserNumber(String ids) {
         return wxEducationUserService.getAllteacherNumber(ids);
    }


    /**
     *获取所有照片审核的同学
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 17:18
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "获取所有照片审核的同学", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/getAllStudentImageCheck", method = RequestMethod.POST)
    public ReturnResult getAllStudentImageCheck(HttpServletRequest request, WxEducationUserVo wxEducationUserVo, int page, int limit) {
        // 根据当前用户查询
        SysUser sysUser = acceptCurrentUser(request);
        wxEducationUserVo.setCreateUser(sysUser.getId());
        return wxEducationUserService.pageNewImageStudent(wxEducationUserVo, page, limit);
    }
    /**
     *单个图片的审核
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 17:18
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "单个图片审核", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/oneImageOfStudentCheck", method = RequestMethod.POST)
    public ReturnResult oneImgeOfStudentCheck(HttpServletRequest request, WxEducationUserVo wxEducationUserVo) {
        // 根据当前用户查询
        SysUser sysUser = acceptCurrentUser(request);
        wxEducationUserVo.setCreateUser(sysUser.getId());
        return  wxEducationUserService.updateOneImage(wxEducationUserVo,sysUser);
    }


    /**
     *多个图片审核
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 17:18
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "多个图片审核", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/moreImageOfStudentCheck", method = RequestMethod.POST)
    public ReturnResult oneImgeOfStudentCheck(HttpServletRequest request, WxEducationUserVo wxEducationUserVo,String ids) {
        // 根据当前用户查询
        SysUser sysUser = acceptCurrentUser(request);
        wxEducationUserVo.setCreateUser(sysUser.getId());
        return  wxEducationUserService.updateMoreImage(wxEducationUserVo,sysUser,ids);
    }


    /**
     *根据工号回去主键id
     *
     * @param ids 搜索关键字
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 17:18
     */
    @BaseLog(module = "学校通用用户模块", classz = "基础用户", methods = "根据工号所对应的主键id", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxeducationuser/getAllIdsByUserNumber", method = RequestMethod.POST)
    public ReturnResult getAllIdsByUserNumber(String ids,HttpServletRequest request) {
        SysUser sysUser = acceptCurrentUser(request);
        return wxEducationUserService.getAllIdsByUserNumber(ids,sysUser.getId());
    }


}